package com.xy.tianyu.module.news.adapter

import android.text.SpannableString
import android.text.Spanned
import android.text.style.AbsoluteSizeSpan
import android.text.style.ForegroundColorSpan
import androidx.core.content.ContextCompat
import com.bumptech.glide.Glide
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.xy.tianyu.R
import com.xy.tianyu.module.news.bean.NewsListMultiItem
import com.xy.tianyu.utils.GlideUtils
import kotlinx.android.synthetic.main.item_news_one_pic.view.*
import kotlinx.android.synthetic.main.item_news_text.view.*
import kotlinx.android.synthetic.main.item_news_three_pic.view.*
import kotlinx.android.synthetic.main.item_news_top.view.*


/**
 * @time 2020/9/18 15:00
 * @class 描述:
 */
class NewsListMultipleAdapter(data:MutableList<NewsListMultiItem>) : BaseMultiItemQuickAdapter<NewsListMultiItem, BaseViewHolder>(data) {
    init {
        addItemType(NewsListMultiItem.NEWS_TOP,R.layout.item_news_top)
        addItemType(NewsListMultiItem.NEWS_TEXT, R.layout.item_news_text)
        addItemType(NewsListMultiItem.NEWS_ONE_PIC,R.layout.item_news_one_pic)
        addItemType(NewsListMultiItem.NEWS_THREE_PIC,R.layout.item_news_three_pic)
    }
    override fun convert(holder: BaseViewHolder, item: NewsListMultiItem) {
        val re = item.re
        when(holder.itemViewType){
            NewsListMultiItem.NEWS_TOP -> with(re){
                val spannableString = SpannableString(context.getString(R.string.ty_topping)+"  "+t)
                spannableString.setSpan(ForegroundColorSpan(ContextCompat.getColor(context,R.color.address_red)), 0, 2, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
                spannableString.setSpan(
                    AbsoluteSizeSpan(10, true),
                    0,
                    2,
                    Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
                )
                spannableString.setSpan(
                    AbsoluteSizeSpan(14, true),
                    3,
                    spannableString.length,
                    Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
                )

                holder.itemView.tv_news_top_title?.text = spannableString
            }

            NewsListMultiItem.NEWS_TEXT -> with(re){
                holder.itemView.tv_news_text_title?.text = t
                holder.itemView.tv_news_text_source?.text = f
            }

            NewsListMultiItem.NEWS_ONE_PIC -> with(re){
                holder.itemView.tv_news_one_pic_title?.text = t
                Glide.with(context).load(i).apply(GlideUtils.getRound10RequestOptions()).into(holder.itemView.iv_news_one_pic)
                holder.itemView.tv_news_one_pic_source?.text = f
            }

            NewsListMultiItem.NEWS_THREE_PIC -> with(re){
                val iList = i.split("|")
                if (iList.size>=3){
                    Glide.with(context).load(iList[0]).apply(GlideUtils.getRound10RequestOptions()).into(holder.itemView.iv_news_three_pic_one)
                    Glide.with(context).load(iList[1]).apply(GlideUtils.getRound10RequestOptions()).into(holder.itemView.iv_news_three_pic_two)
                    Glide.with(context).load(iList[2]).apply(GlideUtils.getRound10RequestOptions()).into(holder.itemView.iv_news_three_pic_three)
                }
                holder.itemView.tv_news_three_pic_title?.text = t
                holder.itemView.tv_news_three_pic_source?.text = f
            }
        }
    }
}